package com.huike.web.controller.system;

import com.huike.clues.mapper.SysDeptMapper;
import com.huike.clues.service.ISysDeptService;
import com.huike.common.config.BaseContext;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.TreeSelect;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.web.result.RoleDeptTreeAjaxResult;
import com.jhlabs.vecmath.AxisAngle4f;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("system/dept")
public class SysDeptController {
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @GetMapping("list")
    public AjaxResult<List<SysDept>> list(String deptName,String status) {
        List<SysDept> list = sysDeptMapper.list(deptName,status);
        if (deptName!=null||status!=null){
            HashSet<SysDept> sysDepts = new HashSet<>();
            for (SysDept sysDept : list) {
                List<SysDept> list1=sysDeptMapper.findByIancestors(sysDept.getDeptId());
                list1.stream().peek(o->sysDepts.add(o)).collect(Collectors.toList());
                sysDepts.add(sysDept);
            }
            System.out.println(sysDepts);
            return AjaxResult.success(sysDepts);
        }
        return AjaxResult.success(list);
    }

    @PostMapping
    public AjaxResult save(@RequestBody SysDept sysDept) {
        sysDept.setCreateBy(BaseContext.getCurrentUser().getCreateBy());
        sysDept.setUpdateBy(BaseContext.getCurrentUser().getUpdateBy());
        sysDept.setUpdateTime(new Date());
        sysDept.setCreateTime(new Date());
        deptService.save(sysDept);
        return AjaxResult.success();
    }
    @PutMapping
    public AjaxResult update(@RequestBody SysDept sysDept){
        sysDept.setUpdateBy(BaseContext.getCurrentUser().getUpdateBy());
        sysDept.setUpdateTime(new Date());
        deptService.update(sysDept);
        return AjaxResult.success();
    }
    @GetMapping("{deptId}")
    public AjaxResult findById(@PathVariable("deptId") Long deptId){
        SysDept sysDept=deptService.findById(deptId);
        return AjaxResult.success(sysDept);
    }
    @DeleteMapping("{deptId}")
    public AjaxResult deleteById(@PathVariable("deptId") Long deptId){
        deptService.deleteById(deptId);
        return AjaxResult.success();
    }
    @GetMapping("treeselect")
    public AjaxResult<TreeSelect> findTreeDept(){
        List<TreeSelect> list=deptService.findTreeDept();
        return AjaxResult.success(list);
    }
    @GetMapping("roleDeptTreeselect/{roleId}")
    public RoleDeptTreeAjaxResult roleDeptTreeselect(@PathVariable("roleId") Long roleId){
        List<Integer> depts=deptService.findRoleDept(roleId);
        RoleDeptTreeAjaxResult roleDeptTreeAjaxResult=new RoleDeptTreeAjaxResult(200,"成功");
        roleDeptTreeAjaxResult.setDepts(deptService.findTreeDept());
        roleDeptTreeAjaxResult.setCheckedKeys(depts);
        return roleDeptTreeAjaxResult;
    }
    @GetMapping("list/exclude/{roleId}")
    public AjaxResult<List<SysDept>> exclude(@PathVariable("roleId") Long deptId) {
        List<SysDept> list = sysDeptMapper.list(null,null);
        List<SysDept> sysDeptList = list.stream().filter(o -> !o.getDeptId().equals(deptId)).collect(Collectors.toList());
        return AjaxResult.success(sysDeptList);
    }
}
